﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Golf.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace Golf.DAL
{
    /// <summary>
    /// 数据访问类:Golf_AdminGroups
    /// </summary>
    public partial class Golf_AdminGroups : IGolf_AdminGroups
    {
        public Golf_AdminGroups()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id", "Golf_AdminGroups");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            int result = DbHelperSQL.RunProcedure("Golf_AdminGroups_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(Golf.Model.Golf_AdminGroups model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@GroupName", SqlDbType.NVarChar,20),
					new SqlParameter("@GroupIntro", SqlDbType.NVarChar,100),
					new SqlParameter("@PowerIdList", SqlDbType.NVarChar,2000),
					new SqlParameter("@AddTime", SqlDbType.DateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.GroupName;
            parameters[2].Value = model.GroupIntro;
            parameters[3].Value = model.PowerIdList;
            parameters[4].Value = model.AddTime;

            DbHelperSQL.RunProcedure("Golf_AdminGroups_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public bool Update(Golf.Model.Golf_AdminGroups model)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@GroupName", SqlDbType.NVarChar,20),
					new SqlParameter("@GroupIntro", SqlDbType.NVarChar,100),
					new SqlParameter("@PowerIdList", SqlDbType.NVarChar,2000),
					new SqlParameter("@AddTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.GroupName;
            parameters[2].Value = model.GroupIntro;
            parameters[3].Value = model.PowerIdList;
            parameters[4].Value = model.AddTime;

            DbHelperSQL.RunProcedure("Golf_AdminGroups_Update", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            DbHelperSQL.RunProcedure("Golf_AdminGroups_Delete", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Golf_AdminGroups ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Golf.Model.Golf_AdminGroups GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            Golf.Model.Golf_AdminGroups model = new Golf.Model.Golf_AdminGroups();
            DataSet ds = DbHelperSQL.RunProcedure("Golf_AdminGroups_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GroupName"] != null && ds.Tables[0].Rows[0]["GroupName"].ToString() != "")
                {
                    model.GroupName = ds.Tables[0].Rows[0]["GroupName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["GroupIntro"] != null && ds.Tables[0].Rows[0]["GroupIntro"].ToString() != "")
                {
                    model.GroupIntro = ds.Tables[0].Rows[0]["GroupIntro"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PowerIdList"] != null && ds.Tables[0].Rows[0]["PowerIdList"].ToString() != "")
                {
                    model.PowerIdList = ds.Tables[0].Rows[0]["PowerIdList"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,GroupName,GroupIntro,PowerIdList,AddTime ");
            strSql.Append(" FROM Golf_AdminGroups ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,GroupName,GroupIntro,PowerIdList,AddTime ");
            strSql.Append(" FROM Golf_AdminGroups ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Golf_AdminGroups";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  Method
    }
}

